package com.hexacta.hrs.sandbox.tasks2.model;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/*
 * Actividad que realiza un usuario
 * 
 * 
 */
@Entity
public class Activity {

	private static final long serialVersionUID = 6430623703467941209L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	protected Long id;

	@Column(length = 45, nullable = false)
	private String name;

	@OneToMany(targetEntity = Task.class, cascade = CascadeType.ALL)
	private List<Task> tasks = new ArrayList<Task>();

	@Column(nullable = false)
	private Date date;

	public Activity(String newName, Date date) {
		this.setName(newName);
		this.setDate(date);
	}

	public Activity() {

	}

	public String getName() {

		return this.name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String newName) {
		this.name = newName;

	}

	public void addTask(Task task) {
		this.getTasks().add(task);

	}

	public List<Task> getTasks() {
		return this.tasks;
	}

	/**
	 * calculates the duration of this activity according to its tasks
	 * @return
	 */
	public int timeLength() {
		int timeLength = 0;

		for (Iterator<Task> iterator = this.tasks.iterator(); iterator
				.hasNext();) {
			Task task = (Task) iterator.next();
			timeLength += task.getTimeLength();
		}

		return timeLength;
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}
